package com.lsh.airticket.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.Airport;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.AirportService;
import com.lsh.airticket.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lsh
 * @date 2021/2/17
 **/
@RestController
@RequestMapping("/flights")
public class AdminAirportController {


    @Autowired
    AirportService airportService;

    @GetMapping("/admin/airport/airportList")
    public Result findAirportList(Integer page) {
        PageHelper.startPage(page, 8);
        PageInfo<Airport> pageInfo = airportService.findAirport();
        return Result.result().addData("pageInfo", pageInfo);
    }

    @PostMapping("/admin/airport/insertAirport")
    public Result insertAirport(@RequestBody Airport airport) {
        int i = airportService.insertAirport(airport);
        if (i != 1) {
            throw new UserException("插入失败！");
        }
        return Result.result().setMsg("添加成功。");
    }

    /**
     * 根据 cityId查找查找 该城市的 机场。
     * @return
     */
    @GetMapping("/admin/airport/findAirportByCityId")
    public Result findAirportByCityId(Integer cityId, Integer page) {
        if (page != null && page > 0) {
            PageHelper.startPage(page, Constants.PAGE_SIZE);
        }
        PageInfo<Airport> pageInfo = airportService.finAirportByCityId(cityId);
        return Result.result().addData("pageInfo", pageInfo);
    }


    /**
     * 删除机场。
     * @return
     */
    @GetMapping("/admin/airport/deleteAirport")
    public Result deleteAirport(Integer airportId) {
        int i = airportService.deleteAirportById(airportId);
        if (i != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }



}
